package org.jeecg;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.client.RestTemplate;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
public class SampleJob implements Job {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    private RestTemplate rt;
    private static ISysBaseAPI sysBaseAPI;
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
       System.out.println("任务开始");
       check13();

    }

    Integer check13(){
        JSONObject j1=new JSONObject();
        String sql="SELECT\n" +
                "\tIFNULL(sum(num), 0) AS num,\n" +
                "\tIFNULL(sum(dnum), 0) AS dnum,\n" +
                "\tIFNULL(sum(hnum), 0) AS hnum,id\n" +
                "FROM\n" +
                "\t(\n" +
                "\t\tSELECT\n" +
                "\t\t\ts.id,\n" +
                "\t\t\ts.fnumber,\n" +
                "\t\t\tg.id AS detailid,\n" +
                "\t\t\tg.num,\n" +
                "\t\t\tg.dnum,\n" +
                "\t\t\tg.hnum\n" +
                "\t\tFROM\n" +
                "\t\t\tshuinidemo.sporder s\n" +
                "\t\tLEFT JOIN gxcp g ON s.id = g.sporderid\n" +
                "\t) f\n" +
                "GROUP BY\n" +
                "\tf.id,\n" +
                "\tf.fnumber,\n" +
                "\tf.detailid";
        List<Map<String, Object>> list1 =  jdbcTemplate.queryForList(sql);
        j1.put("data",list1);
        JSONArray data=j1.getJSONArray("data");
        for (int j=0;j<data.size();j++){
            Integer num=data.getJSONObject(j).getInteger("num");
            Integer dnum=data.getJSONObject(j).getInteger("dnum");
            Integer hnum=data.getJSONObject(j).getInteger("hnum");
            System.out.println(num);
            System.out.println(dnum);
            System.out.println(hnum);
            if (hnum+dnum>=num){
                 System.out.println("执行");
                String updatesql="update shuinidemo.sporder set wgzt='已完工' where id='"+data.getJSONObject(j).getString("id")+"'";
                jdbcTemplate.execute(updatesql);
            }

        }

        return 0;
    }
}
